package wayden;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;

class Description {

    private String id2;
    private String name;
    public Description(String id, String name) {
	super();
	this.id2 = id;
	this.name = name;
    }

    public String getId() {
	return id2;
    }

    public void setId(String id) {
	this.id2 = id;
    }
    
    public String getName() {
	return this.name;
    }

    public void setName(String name) {
	this.name = name;
    }

}

public class Friend {

    private String name;
    private String id;
    private String id2;
    



	List aa = new ArrayList<Description>();
    Map session;
    public void setName(String name) {
	this.name = name;
    }

    public String getId2() {
		return id2;
	}

	public void setId2(String id2) {
		this.id2 = id2;
	}
    
    public String getName() {
	return this.name;
    }

    public void setId(String id) {
	this.id = id;
    }

    public String getId() {
	return this.id;
    }

    public void setAa() {

	this.aa.add("a");

    }

    public List getAa() {
	return this.aa;
    }

    public String friend() throws ClassNotFoundException, SQLException {
	//Friends friend = new Friends();
    
    session = ActionContext.getContext().getSession();
    	if (session.get("user")!=null) {
    	setId(session.get("user").toString());
	String q_friend = "select * from friends where id_user = " + //getId();
		session.get("user").toString();
	//System.err.out();
	String q_user = "select * from users where id = ?";
	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();
	Statement st = conn.createStatement();
	PreparedStatement ps;
	st.executeQuery(q_friend);
	ResultSet rs = st.getResultSet();
	this.id = session.get("user").toString();
	if (session.containsKey("user2"))
	this.id2 = session.get("user2").toString();
	System.err.println("_"+getId2());
	//System.err.println("**"+getId());
	ResultSet rs2;

	while (rs.next()) {

	    String id_user = rs.getString(3);
	    ps = conn.prepareStatement(q_user);
	    ps.setString(1, id_user);
	    ps.execute();
	    rs2 = ps.getResultSet();
	    if (rs2.next()) {
		aa.add(new Description(rs2.getString(1), rs2.getString(4)));
	    }
	}

	return "success";
    }
    	else return "error";
    }

    public String add() throws ClassNotFoundException, SQLException {
    session = ActionContext.getContext().getSession();
    if (session.get("user")!=null) {
	String search = "select * from users where login = ?";
	String insert = "insert into friends (id_user, id_friend) values (?,?)";
	String check = "select * from friends where id_user = ? and " +
			"id_friend = ?";
	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();

	String Name = getName();
	String Id = session.get("user").toString(); 
	this.id = Id;

	PreparedStatement ps;
	ps = conn.prepareStatement(search);
	ps.setString(1, name);
	
	ps.execute();
	ResultSet rs = ps.getResultSet();
	if (rs.next()) {
	    String id_friend = rs.getString(1);
	    if (!id_friend.equals(Id)) {
	    ps = conn.prepareStatement(check);
	    ps.setString(1, Id);
	    ps.setString(2, id_friend);
	    ps.execute();
	    rs = ps.getResultSet();
	    if (!rs.next()) {
		    ps = conn.prepareStatement(insert);
		    ps.setString(1, Id);
		    ps.setString(2, id_friend);
		    ps.execute();
		return "success";
	    } 
	    else return "exist";
	    } else return "yourself";
	}
	else return "error";
	
    }
    else return "error";
    }
    
    
    public String delete() throws ClassNotFoundException, SQLException {
        session = ActionContext.getContext().getSession();
    	String delete = "delete from friends where id_user = ? and id_friend = ?";

    	Connection conn = null;
    	ConnectDB connectDB = new ConnectDB();
    	conn = connectDB.Connect();

    	String Name = getName();
    	String Id = session.get("user").toString(); 
    	this.id = Id;

    	PreparedStatement ps;
    	ps = conn.prepareStatement(delete);
    	ps.setString(1, getId());
    	ps.setString(2, getId2());
    	
    	ps.execute();
    	return "success";
    
    }
    
    
    public String view() throws ClassNotFoundException, SQLException {
    	session = ActionContext.getContext().getSession();
    	System.err.println("__________"+getId2());
    	session.put("user2", getId2());
    	return "success";
    
    }
    
    
}
